<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2020/5/12
 * Time: 16:37
 */

namespace App\Repositories;

use App\PatentApply;
use App\Repositories\Eloquent\Repository;
use App\Traits\SystemCodeTrait;
use Encore\Admin\Facades\Admin;

class PatentApplyRepository extends Repository
{
    use SystemCodeTrait;

    function model()
    {
        return PatentApply::class;
    }
    //用户
    public function userGrid($orderColumn = 'id', $order = 'desc')
    {
        $user = Admin::user();
        $grid = $this->grid();
        $grid->model()->where(function ($query) use ($user) {
            $user_name = $user['name'];
            $query->where('user_id', $user->id)->orWhere('inventor', 'like', "%$user_name%");
        })->orderBy($orderColumn, $order);
        return $grid;
    }

    public function getNewCode($type = ''): string
    {
        $pre = $type ?: config('app.uuid_prefix', 'SN');
        $uuid = $this->model->whereDate('created_at', '=', date('Y-m-d'))->orderByDesc('id')->value('uuid');
        if (!$uuid) {
            $code = date('Ymd') . str_pad(1, 4, 0, STR_PAD_LEFT);
        } else {
            $code = substr($uuid, -12) + 1;
        }
        //HNDX202201200001
        return $pre . $code;
    }
}
